package com.food.admin.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.food.admin.dto.AccountDto;
import com.food.admin.dto.AccountListDto;
import com.food.admin.entity.Account;
import com.food.admin.entity.AccountInfo;
import com.food.admin.entity.ProductsType;
import com.food.admin.exception.accountException.AccountException;
import com.food.admin.exception.accountException.AccountExceptionCode;
import com.food.admin.form.*;
import com.food.admin.mapper.AccountInfoMapper;
import com.food.admin.mapper.AccountMapper;
import com.food.admin.model.ConstantData;
import com.food.admin.service.AccountService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.food.admin.util.JwtUtil;
import com.food.admin.util.MyPage;
import com.food.admin.util.RedisUtil;
import com.food.admin.util.WebUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 帅得一
 * @since 2023年06月13日
 */
@Service
public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account> implements AccountService {

    @Autowired
    private AccountMapper accountMapper;
    @Autowired
    private AccountInfoMapper accountInfoMapper;
    @Autowired
    private RedisUtil redisUtil;
    @Override
    public AccountDto login(AccountLoginForm form) throws Exception {
        //查询用户名匹配且状态正常的管理员信息
        //创建QueryWrapper对象
        //拼接用户名条件
        QueryWrapper<Account> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("account_name",form.getAccountName());
        Account account = accountMapper.selectOne(queryWrapper);
        //账号不存在
        if(account==null){
           throw new AccountException(AccountExceptionCode.ACCOUNT_NOT_EXIST);
        }
        //密码错误
        if(!account.getAccountPassword().equals(form.getAccountPassword())){
            throw new AccountException(AccountExceptionCode.ACCOUNT_PASSWORD_ERROR);
        }
        //账号状态是冻结和删除
        if(account.getAccountState()!= ConstantData.ACCOUNT_STATE_NORMAL){
            throw new AccountException(AccountExceptionCode.ACCOUNT_STATE_ERROR);
        }
        AccountDto accountDto = BeanUtil.toBean(account, AccountDto.class);
        return accountDto;
    }

    @Override
    public Account insert(AccountInsertForm form) throws Exception {
        //管理用户名不能重复
        //创建QueryWrapper对象
        QueryWrapper<Account> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("account_phone",form.getAccountPhone());
        Account account = accountMapper.selectOne(queryWrapper);
        if(account!=null){
            throw new AccountException(AccountExceptionCode.ACCOUNT_IS_EXIST);
        }
        Account acc = BeanUtil.toBean(form, Account.class);
        acc.setAccountState(ConstantData.ACCOUNT_STATE_NORMAL);
        acc.setAccountRole(ConstantData.ACCOUNT_ROLE_NORMAL);
        acc.setAccountRoleName(ConstantData.ACCOUNT_ROLE_NORMAL_NAME);
        //默认都不能做
        acc.setAccountPowerId(ConstantData.ACCOUNT_POWER_LEVEL4);
        String accountName = "admin"+form.getAccountPhone().substring(form.getAccountPhone().length()-4);
        acc.setAccountName(accountName);
        acc.setAccountPassword(form.getAccountPhone().substring(form.getAccountPhone().length()-6));
        acc.setAccountNickName("默认名字");
        accountMapper.insert(acc);
        System.out.println(acc);
        AccountInfo accountInfo = BeanUtil.toBean(acc, AccountInfo.class);
        accountInfo.setAccountId(acc.getAccountId());
        accountInfo.setAccountPhone(acc.getAccountPhone());
        accountInfoMapper.insert(accountInfo);
        return acc;
    }

    @Override
    public Account updateState(AccountUpdateStateForm form) throws Exception {
        Account account = accountMapper.selectById(form.getAccountId());
        //判断是否存在
        if(account==null){
            throw new AccountException(AccountExceptionCode.ACCOUNT_NOT_EXIST);
        }
        if(form.getAccountState()==ConstantData.ACCOUNT_STATE_DISABLED){
            if(account.getAccountState()==ConstantData.ACCOUNT_STATE_DISABLED){
                throw new AccountException(AccountExceptionCode.ACCOUNT_YIBEIJINGYONG);
            }
            if (account.getAccountState()==ConstantData.ACCOUNT_STATE_DELETE){
                throw new AccountException(AccountExceptionCode. ACCOUNT_YIBEISHANCHU_HUIFU);
            }
        }else if (form.getAccountState()==ConstantData.ACCOUNT_STATE_DELETE){
            if (account.getAccountState()==ConstantData.ACCOUNT_STATE_DELETE){
                throw new AccountException(AccountExceptionCode. ACCOUNT_YIBEISHANCHU);
            }
        }else if(form.getAccountState()==ConstantData.ACCOUNT_STATE_NORMAL){
            if (account.getAccountState()==ConstantData.ACCOUNT_STATE_NORMAL){
                throw new AccountException(AccountExceptionCode. ACCOUNT_YIBEIQIYONG);
            }
        }
        Account acc = BeanUtil.toBean(form, Account.class);
        accountMapper.updateById(acc);
        return acc;
    }

    @Override
    public void updatePassword(AccountUpdatePasswordForm form) throws Exception {
        String accessToken = WebUtil.getRequest().getHeader("accessToken");
        String refreshToken = redisUtil.get(accessToken);
        Integer id = JwtUtil.getUserId(refreshToken);
        Account account = accountMapper.selectById(id);
        if (!form.getOldPassword().equals(account.getAccountPassword())){
            throw new AccountException(AccountExceptionCode. UPDATE_PASSWORD_OLD_ERROR);
        }
        if (form.getNewPassword().equals(account.getAccountPassword())){
            throw new AccountException(AccountExceptionCode.UPDATE_PASSWORD_OLD_NEW);
        }
        account.setAccountPassword(form.getNewPassword());
        accountMapper.updateById(account);
    }

    @Override
    public void updateNickname(AccountUpdateNicknameForm form) throws Exception {
        String accessToken = WebUtil.getRequest().getHeader("accessToken");
        String refreshToken = redisUtil.get(accessToken);
        Integer id = JwtUtil.getUserId(refreshToken);
        Account account = accountMapper.selectById(id);
        if (form.getAccountNickName().equals(account.getAccountNickName())){
            throw new Exception("新老昵称相同");
        }
        account.setAccountNickName(form.getAccountNickName());
        accountMapper.updateById(account);
    }

    @Override
    public void outLogin() throws Exception {
        String accessToken1 = WebUtil.getRequest().getHeader("accessToken");
        System.out.println(accessToken1);
        redisUtil.del(accessToken1);
        System.out.println("退出登录删除token");
    }

    @Override
    public List<AccountListDto> listAll(AccountListPageForm form) throws Exception {
        List<Account> list =list(new QueryWrapper<Account>().ne("account_role",0));
        ArrayList<AccountListDto> accountListDtos = new ArrayList<>();
        for (Account account : list) {
            AccountListDto accountListDto = BeanUtil.toBean(account, AccountListDto.class);
            if(account.getAccountState()==0) accountListDto.setStateName("删除状态");
            if(account.getAccountState()==1) accountListDto.setStateName("正常状态");
            if(account.getAccountState()==2) accountListDto.setStateName("冻结状态");
            if(account.getAccountPowerId()==1) accountListDto.setPowerName("超级管理员");
            if(account.getAccountPowerId()==2) accountListDto.setPowerName("审核注册员");
            if(account.getAccountPowerId()==3) accountListDto.setPowerName("类别管理员");
            if(account.getAccountPowerId()==4) accountListDto.setPowerName("工作人员");

            accountListDtos.add(accountListDto);
        }

        return accountListDtos;
    }

    @Override
    public void authorization(AccountAuthorizationForm form) {
        Account byId = getById(form.getAccountId());
        if (byId==null){
            throw new AccountException(AccountExceptionCode.ACCOUNT_NOT_EXIST);
        }
        byId.setAccountPowerId(form.getAccountPowerId());
        updateById(byId);
    }
}
